Find minimum value


Posted by Christy on 2022-04-21

Description: Write a function named findMin that accepts an array and find the minimum value

Note: Don’t use built-in function sort()

function findMin(arr) {
  let min = arr[0];
  for (let i = 1; i < arr.length; i++) {
    if (min > arr[i]) {
      min = arr[i];
    }
  }
  return min;
}

console.log(findMin([1, 2, 3])); // 1
console.log(findMin([1, 6, 0, 33, 44, 88, -10])); // -10

Assign a minimum value first, if this value is bigger than any other value in the array, renew the value.










Related Posts

讀書筆記-JavaScript技術手冊3: 函式基本語法

讀書筆記-JavaScript技術手冊3: 函式基本語法

AppWorks School Batch #16 Front-End Class 學習筆記&心得(駐點階段四:個人專案~重構)

AppWorks School Batch #16 Front-End Class 學習筆記&心得(駐點階段四:個人專案~重構)

MTR04 W2 D19 第二週作業

MTR04 W2 D19 第二週作業


Comments